<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .test {
            position: relative;
            width: 500px;
            height: 200px;
            border: 1px solid #f00;
            line-height: 200px;
            font-size: 40px;
            text-align: center;
            box-sizing: border-box;
        }
    </style>
</head>

<body>
    <div class="test" id="test">1234567890</div>
    <script src="html2canvas.min.js"></script>
    <script>
        var forPostImg = document.getElementById('test');
        html2canvas(forPostImg, {
            scale: 1,
            width: 500,
            height: 200,
            allowTaint: true,
            useCORS: true, //使用跨域(当allowTaint为true时这段代码没什么用)
            backgroundColor: "transparent" //设置背景色为透明
        }).then(function(canvas) {
            console.log(canvas);
            var dataUrl = canvas.toDataURL("image/png");
            var img = new Image();
            img.src = dataUrl;
            document.body.append(img);
        });
    </script>
</body>

</html>